// pages/business_info/business_info.js
const app = getApp(), core = require('../../utils/core'), util = require('../../utils/util'), WeValidator = require('../../utils/we-validator');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    industryCategories: [],
    index: 0,
    logo: '',
    license_type: null,
    license_types: ['永久', '日期选择'],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(this.getOpenerEventChannel().on) {
      this.getOpenerEventChannel().on('infoTransfer', data => {
        this.setData({
          info: data.info,
          logo: data.info.logo,
        });
        if(data.info.valid_date=='') {
          this.setData({license_type:null});
        }else if(data.info.valid_date=='永久') {
          this.setData({license_type: 0});
        }else {
          this.setData({license_type: 1});
        }
      });
    }else {
      core.get({
        url: '/enterprise/info',
        success: ret => {
          let info = ret.data.enterprise_info;
          this.setData({
            info: info,
            logo: info.logo,
          });
          if(info.valid_date=='') {
            this.setData({license_type:null});
          }else if(info.valid_date=='永久') {
            this.setData({license_type: 0});
          }else {
            this.setData({license_type: 1});
          }
        }
      });
    }
    util.getIndustryCategories().then(items=>{
      this.setData({industryCategories: items});
    });
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  bindIndustryCategoryChange(event) {
    let selectedItem = event.detail.items[0];
    this.setData({'info.industry_category_id': selectedItem.id});
  },
  uploadLogo() {
    wx.chooseImage({
      count: 1,
      success: res => {
        let file = res.tempFiles[0];
        core.uploadFile({
          filePath: file.path,
          success: (ret, response) => {
            this.setData({'logo': ret.data.url});
          }
        });
      }
    });
  },
  bindSubmit(event) {
    let data = event.detail.value;
    let validator = new WeValidator({
      rules: {
        contact_phone: {mobile:true},
        contact_email: {email:true}
      },
      messages: {
        contact_phone: {mobile:'请填写正确的手机号'},
        contact_email:{email:'请填写正确的邮箱'},
      }
    });
    if(!validator.checkData(data)) {
      return;
    }
    this.setData({
      'info.full_name': data.full_name,
      'info.industry_category_id': data.industry_category_id,
      'info.contact_person': data.contact_person,
      'info.contact_phone': data.contact_phone,
      'info.contact_email': data.contact_email,
      'info.address': data.address,
      'info.logo': data.logo,
      'info.license_img': data.license_img,
      'info.company_name': data.company_name,
      'info.license_no': data.license_no,
      'info.valid_date': data.valid_date
    });
    if(this.getOpenerEventChannel().emit) {
      this.getOpenerEventChannel().emit('infoTransfer', {info: this.data.info});
      wx.navigateBack();
    }else {
      let keys = ['full_name','industry_category_id','contact_person','contact_phone','contact_email','address','logo',
        'company_scale_id','company_type_id','intro','environment_images','license_img','company_name','license_no','valid_date'];
      let postData = {};
      keys.forEach(key => postData[key] = this.data.info[key]);
      let benefitIds = [];
      this.data.info.benefits.forEach(item=>benefitIds.push(item.id));
      postData.benefit_ids = benefitIds.join(',');
      core.post({
        url: 'enterprise/save_info', data: postData,
        success: ret => {
          // app.eventBus.emit(app.Events.UcenterInfoCompleteChange, ret.data.enterprise_info.info_complete);
          wx.switchTab({url:'/pages/indexpage/indexpage'});
        }
      });
    }
  },

  // 认证信息相关操作
  uploadLicense() {
    // if(![0,3].includes(parseInt(this.data.audit_status)))
    //   return;
    let page = this;
    wx.chooseImage({
      count:1,
      success: res => {
        let file = res.tempFiles[0];
        if(file.size > 10*1024*1024) {
          wx.showToast({title:'文件大小最多10M',icon:'none'});
          return;
        }
        core.uploadFile({
          filePath: file.path,
          success(ret, response) {
            page.setData({'info.license_img': ret.data.url});
          }
        })
      }
    });
  },
  bindDateChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      'info.valid_date': e.detail.value,
    })
  },
  bindPickerChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value);
    if(e.detail.value==0) {
      this.setData({'info.valid_date': '永久'});
    }else {
      this.setData({'info.valid_date': ''});
    }
    this.setData({
      license_type: e.detail.value,
    })
  },

});
